package org.csu.sei.util;

//import jdk.internal.org.jline.reader.UserInterruptException;
import org.csu.sei.entity.User;
import org.csu.sei.util.interceptor.ADMINISTRATORInterceptor;
import org.csu.sei.util.interceptor.REGULATORInterceptor;
import org.csu.sei.util.interceptor.TEACHERInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    private CommonInterceptor commonInterceptor;
    private TEACHERInterceptor teacherInterceptor;
    private REGULATORInterceptor regulatorInterceptor;
    private ADMINISTRATORInterceptor administratorInterceptor;

    //构造方法
    public WebConfiguration(CommonInterceptor commonInterceptor,
                            TEACHERInterceptor teacherInterceptor,
                            REGULATORInterceptor regulatorInterceptor,
                            ADMINISTRATORInterceptor administratorInterceptor){
        this.commonInterceptor = commonInterceptor;
        this.teacherInterceptor = teacherInterceptor;
        this.regulatorInterceptor = regulatorInterceptor;
        this.administratorInterceptor = administratorInterceptor;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("*");
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer){
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
//        List<String> excludePath = new ArrayList<>();
        //排除拦截，除了注册登录(此时还没token)，其他都拦截
        //设置拦截请求
//        registry.addInterceptor(commonInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns("/**");//测试不拦截
//                .excludePathPatterns("/error")
//                user放行



        registry.addInterceptor(regulatorInterceptor)
                .addPathPatterns("/category/**")
                .excludePathPatterns("/category/router")
                .excludePathPatterns("/category/{id}/recentUpdateTime")
                .excludePathPatterns("/category/all")
                .excludePathPatterns("/category/bar")
                .excludePathPatterns("/category/all")
                .excludePathPatterns("/category/bar")
                .excludePathPatterns("/category/{catId}")
                .excludePathPatterns("/category/{id}/all")
                .excludePathPatterns("/category/{id}/children")
                //news不拦截
                .addPathPatterns("/news/**")
                .excludePathPatterns("/news/latest")
                .excludePathPatterns("/news/categoryList/{catId}")
                .excludePathPatterns("/news/newsList")
                .excludePathPatterns("/news/{newsId}")
                //banner不拦截
                .addPathPatterns("/banner/**")
                .excludePathPatterns("/banner/all")
                .excludePathPatterns("/banner/{id}")
                //user
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/accounts")
                .excludePathPatterns("/user/info")
                .excludePathPatterns("/user/{id}/**")
                .excludePathPatterns("/user/account")
                .excludePathPatterns("/user/password")
                .excludePathPatterns("/user//password_reset")
                .excludePathPatterns("/user/reset_token")
                .excludePathPatterns("/user/token");


        registry.addInterceptor(teacherInterceptor)
                .addPathPatterns("/teacher/**")
                .excludePathPatterns("/teacher/{id}")
                //user不拦截
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/accounts")
                .excludePathPatterns("/user/info")
                .excludePathPatterns("/user/{id}/**")
                .excludePathPatterns("/user/account")
                .excludePathPatterns("/user/password")
                .excludePathPatterns("/user//password_reset")
                .excludePathPatterns("/user/reset_token")
                .excludePathPatterns("/user/token");

//                .excludePathPatterns("/**");//测试不拦截
        registry.addInterceptor(administratorInterceptor)
                .addPathPatterns("/category/**")
                .excludePathPatterns("/category/router")
                .excludePathPatterns("/category/{id}/recentUpdateTime")
                .excludePathPatterns("/category/all")
                .excludePathPatterns("/category/bar")
                .excludePathPatterns("/category/all")
                .excludePathPatterns("/category/bar")
                .excludePathPatterns("/category/{catId}")
                .excludePathPatterns("/category/{id}/all")
                .excludePathPatterns("/category/{id}/children")
                //news不拦截
                .addPathPatterns("/news")
                .excludePathPatterns("/news/latest")
                .excludePathPatterns("/news/categoryList/{catId}")
                .excludePathPatterns("/news/newsList")
                .excludePathPatterns("/news/{newsId}")
                //banner不拦截
                .addPathPatterns("/banner")
                .excludePathPatterns("/banner/all")
                .excludePathPatterns("/banner/{id}")
                //teacher不拦截
                .addPathPatterns("/teacher/**")
                .excludePathPatterns("/teacher/{id}")
                //user不拦截
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/accounts")
                .excludePathPatterns("/user/info")
                .excludePathPatterns("/user/{id}/**")
                .excludePathPatterns("/user/account")
                .excludePathPatterns("/user/password")
                .excludePathPatterns("/user//password_reset")
                .excludePathPatterns("/user/reset_token")
                .excludePathPatterns("/user/token");

        WebMvcConfigurer.super.addInterceptors(registry);
    }
}